package com.jiang.thread.thread;

/**
 * 线程停止     使用标志位
 * 线程休眠     sleep(millis)
 * 线程礼让     yield()
 * 线程强制执行 join()
 */
public class ThreadFeatures {
    public static void main(String[] args) throws InterruptedException {
//        new ThreadPause().doThreadStop();
//        new ThreadYield().doYield();
        new ThreadJoin().doQueue();
//        new ThreadState().doTestState();
    }
}

/**
 * 1.建议线程正常停止，利用次数，不建议死循环
 * 2.建议使用标志位，设置一个标志位
 * 3.不要使用 stop 或者 destroy 等过时或者JDK不建议使用的方法
 */
class ThreadPause implements Runnable{

    boolean flag = true; // 标志位

    @Override
    public void run() {
        int i = 1;
        while(flag){
            System.out.println(Thread.currentThread().getName() + " " + i++);
        }
    }

    // 公共的线程停止方法，通过标志位转换
    public void stop(){
        this.flag = false;
    }

    public void doThreadStop(){
        ThreadPause threadPause = new ThreadPause();
        new Thread(threadPause, "pause").start();
        for (int j = 1; j <= 100; j++) {
            System.out.println("main " + j);
            if(j == 50){
                threadPause.stop();
                System.out.println("pause线程停止了");
            }
        }
    }
}

/**
 * 线程礼让，让正在执行的线程停止，但不阻塞
 * 将线程从 运行状态 转为 就绪状态
 * 礼让不一定成功，看CPU心情
 */
class ThreadYield implements Runnable{
    @Override
    public void run() {
        String name = Thread.currentThread().getName();
        if(name == "车辆") {
            Thread.yield();
        }
        System.out.println(name + "过马路了");
    }

    public void doYield(){
        ThreadYield threadYield = new ThreadYield();
        new Thread(threadYield, "车辆").start();
        new Thread(threadYield, "小孩").start();
        new Thread(threadYield, "老人").start();
    }
}

/**
 * 线程强制执行：join合并线程，待此线程执行完成后，再执行其他线程（其他线程阻塞）
 * 整个过程可以想象成：vip、普通两个线程，待二者就绪后，vip插在队头执行，然后执行普通线程
 */
interface ThreadQueue extends Runnable{}

class ThreadJoinALevel implements ThreadQueue{
    @Override
    public void run() {
        for (int i = 0; i < 20; i++) {
            System.out.println(Thread.currentThread().getName() + i + " 安安静静排队");
        }
    }
}

class ThreadJoinVipLevel implements ThreadQueue{
    @Override
    public void run() {
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + i +" 直接插队。。");
        }
    }
}

class ThreadJoin{
    public void doQueue(){
        ThreadQueue aLevel = new ThreadJoinALevel();
        ThreadQueue vipLevel = new ThreadJoinVipLevel();
        Thread threaALevel = new Thread(aLevel, "白嫖党");
        Thread threaVipLevel = new Thread(vipLevel, "VIP~");
        threaVipLevel.start();  // vip 线程要先于其他线程就绪，这样才能给它强制执行的优先权
        try {
            threaVipLevel.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        threaALevel.start();
    }
}

/**
 * 线程状态
 */
class ThreadState{
    public void doTestState() throws InterruptedException{
        Runnable runnable = ()->{
            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
        };
        Thread thread1 = new Thread(runnable,"a");
        Thread thread2 = new Thread(runnable,"b");
        System.out.println(thread1.getState());     // NEW
        thread1.start();
        thread2.start();
        System.out.println(thread1.getState());     // RUNNABLE
        while(thread1.getState()!=Thread.State.TERMINATED){
            Thread.sleep(300);
            System.out.println(thread1.getState());     // RUNNABLE / BLOCKED / TIMED_WAITING / TERMINATED
        }
        System.out.println(thread1.getState());     // TERMINATED
    }
}





